package com.pigeon.crm.comm;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pigeon.crm.entity.CrmDictionary;
import com.pigeon.crm.entity.CrmUsers;

public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao {

	public <T> void delete(T t) throws HibernateException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(t);
	}

	public <T> void update(T t) throws HibernateException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(t);
	}

	public <T> T save(T t) throws HibernateException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(t);
		return t;
	}

	public <T> T savaOrUpdate(T t) throws HibernateException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(t);
		return t;
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> findByHql(String hql, Object... objects)
			throws HibernateException {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find(hql, objects);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public <T> List<T> findByHqlWithPage(final String hql, final int start,
			final int max) throws HibernateException {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().executeFind(
				new HibernateCallback<List>() {
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						// if (values != null && values.size() > 0) {
						// for (int i = 0; i < values.size(); i++) {
						// query.setParameter(i, values.get(i));
						// }
						// }
						query.setFirstResult((start - 1) * max);
						query.setMaxResults(max);
						return query.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	public <T> List<CrmDictionary> findDictionryHql(String hql,
			Object... objects) throws HibernateException {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find(hql, objects);
	}

	public int getAllRowCount(String hql) throws HibernateException {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find(hql).size();
	}

	public List queryForPage(final String hql, final int offset,
			final int lenght) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(offset);
				query.setMaxResults(lenght);
				List list = query.list();
				return list;
			}
		});
		return list;
	}

	public <T> void update(final String hql, final Object... objects)
			throws HibernateException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().execute(new HibernateCallback<T>() {
			public T doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Query query = session.createQuery(hql);
				if (objects != null) {
					for (int i = 0; i < objects.length; i++) {
						query.setParameter(i, objects[i]);
					}
				}
				query.executeUpdate();
				return null;
			}

		});

	}

	public <T> T get(Class<T> cls, Serializable id) throws HibernateException {
		return this.getHibernateTemplate().get(cls, id);
	}

}
